package com.hubeiweb.controller;

import com.fasterxml.jackson.core.JsonParser;
import com.google.gson.Gson;
import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class TreeController implements HttpRequestHandler {

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        System.out.println(name);
        //返回json格式
        Map<String, Object>map = new HashMap<String, Object>();
        map.put("name", "刘备");
        map.put("age", 18);
        //把map这个java转变成json串发送到客户端
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        Gson gson = new Gson();
        String result=gson.toJson(map);
        PrintWriter writer = response.getWriter();
        writer.print(result);
        writer.close();


    }
}
